layui.use(['layer','table','element','jquery'],function () {
    var layer = layui.layer;
    var table = layui.table;
    var element  = layui.element;
    var $  = layui.$;
    table.render({
        elem:'#coupons',
        url:'/Coupons/selectCoupons?couponName=&online=&grant=',
        toolbar:true,
        title:'优惠券列表',
        page: {
            limit: 5,
            layout: ['count', 'prev', 'page', 'next', 'skip', 'refresh'],
            first: "首页",
            last: "尾页",
        },
        cols:[[
            {type: 'radio', with: 50, fixed: 'left'},
            {field: '', title: '序号', align: 'center', templet: function (data) {
                    return data.LAY_INDEX;}},
            {field: 'couponCode', title: '优惠券编号', align: 'center'},
            {field: 'couponName', title: '优惠券名称', align: 'center'},
            {field: 'couponPrice', title: '优惠券金额', align: 'center',templet:function (data) {
                    return data.couponPrice+"元";
                }},
            {field: 'startDate', title: '开始时间', align: 'center'},
            {field: 'endDate', title: '结束时间', align: 'center'},
            {field: '', title: '剩余数量', align: 'center',templet:function (data) {
                    return data.issueQuantity - data.used;
                }},
            {field: 'type', title: '优惠券类型', align: 'center',templet:function (data) {
                    if (data.type==2){
                        return "商品券";
                    } else if (data.type==3) {
                        return "视频券"
                    }else {
                        return "通用券";
                    }
                }},
        ]]
    });
    var couponId;
    table.on('radio(test)',function (obj) {
        couponId = obj.data.couponId;
        //确定
        $("#determine").click(function () {
            add();
        });
    });
    function add() {
        var getDate = new Date();
        getDate = getTime(getDate);
        var userId = $('input[name=userId]').val();
        var json = {
            'couponId':couponId,
            'getDate':getDate,
            'remarks':2,
            'userId':userId
        };
        $.ajax({
            type:'post',
            javaType:"json",
            data:json,
            url:"/Coupons/addBeforeCoupons",
            success:function (data) {
                if(data){
                    layer.msg("赠送成功");

                }else{
                    layer.msg("赠送失败");
                }
            }
        })
        var layer=parent.layer;
        layer.close(layer.index);
    }
    //顶部时间
    function getTime(date){
        var year = date.getFullYear(); //获取完整的年份(4位,1970-????)
        var month = date.getMonth()+1; //获取当前月份(0-11,0代表1月)
        var today = date.getDate(); //获取当前日(1-31)
        var hour = date.getHours(); //获取当前小时数(0-23)
        var minute = date.getMinutes(); //获取当前分钟数(0-59)
        var second = date.getSeconds(); //获取当前秒数(0-59)
        var nowTime;
        nowTime = year+'-'+fillZero(month)+'-'+fillZero(today)+' '+fillZero(hour)+':'+fillZero(minute)+':'+fillZero(second);
        return nowTime;
    };

    //时间的为零操作
    function fillZero(str){
        var realNum;
        if(str<10){
            realNum	= '0'+str;
        }else{
            realNum	= str;
        }
        return realNum;
    };
    //取消
    $("#cancel").click(function () {
        var layer=parent.layer;
        layer.close(layer.index);
    });
});